package com.cuckoo.cuckoo_app2;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Messenger;
import android.util.Log;

import com.cuckoo.cuckoo_app2.dao.User;
import com.cuckoo.cuckoo_app2.dao.VersionControl;
import com.cuckoo.cuckoo_app2.greendao.DaoMaster;
import com.cuckoo.cuckoo_app2.greendao.DaoSession;
import com.cuckoo.cuckoo_app2.utils.NotificationUtils;
import com.cuckoo.cuckoo_app2.utils.SignCheckUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class MyApplication extends Application {
    public static MyApplication instances;
    public NotificationUtils notificationUtils;
    private Messenger mMessenger;
    private DaoMaster.DevOpenHelper devOpenHelper;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;

    // 线程池
    private ScheduledExecutorService scheduler;

    // 全局内存数据
    private User user;
    private Map<String, VersionControl> versionMap;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("TEST", "MyApplication.onCreate()");

        if (BuildConfig.IS_DEBUG) {
            // 这里是开发环境特有的代码
            Log.d("TAG", "这是一个调试信息");
        } else {
            // 这里是生产环境特有的代码
            Log.d("TAG", "这是一个生产信息");
            this.verifysignature();
        }

        this.instances = this;
        this.context = this;

        // 初始化 GreenDAO
        this.devOpenHelper = new DaoMaster.DevOpenHelper(this, "cuckoo-db", null);
        this.db = this.devOpenHelper.getWritableDatabase();
        this.daoMaster = new DaoMaster(this.db);
        this.daoSession = this.daoMaster.newSession();
        this.notificationUtils = new NotificationUtils(this);

        // 创建10个线程
        this.scheduler = Executors.newScheduledThreadPool(4);
    }

    public static MyApplication getInstances() {
        return instances;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public NotificationUtils getNotificationUtils() {
        return notificationUtils;
    }

    public void setmMessenger(Messenger mMessenger) {
        this.mMessenger = mMessenger;
    }

    public Messenger getMessage() {
        return mMessenger;
    }

    public Context getContext() {
        return context;
    }

    /**
     * 获取线程池
     * @return
     */
    public ScheduledExecutorService getScheduler() {
        return scheduler;
    }


    /**
     * 验证签名的正确性。
     * 本方法使用SignCheckUtils工具类来检查当前应用的签名是否符合预设的签名信息。
     * 签名验证用于确保应用的完整性，防止应用被篡改。
     */
    public void verifysignature() {
        // 创建SignCheckUtils实例，传入当前应用实例和预设的签名字符串。
        SignCheckUtils signCheck = new SignCheckUtils(this, "DA:AE:D2:17:B7:81:DF:01:F7:DB:AF:9B:EF:EB:44:78:83:50:A3:5A");
        // 检查签名是否有效
        if (signCheck.check()) {
            // 签名有效时的处理，这里仅输出日志信息。
            //TODO 签名正常
            Log.i("SIGN", "signature_SUCCESS");
        } else {
            // 签名无效时的处理，这里仅输出日志信息。
            //TODO 签名不正确
            Log.i("SIGN", "signature_ERROR");
            // 加载自定义的异常处理器
            System.exit(0);
        }
    }

    /**
     * 全局内存变量获取
     * 如果token 过去获取不到用户信息
     *
     * @return
     */
    public User getUser() {
        if (user == null) {
            List<User> users = getDaoSession().getUserDao().queryBuilder().list();
            if (users.size() > 0) {
                user = users.get(0);
            } else {
                return null;
            }
        }

        if (user.getToken() == null || user.getToken().isEmpty()) {
            return null;
        }
        return user;
    }

    /**
     * 清除user缓存
     */
    public void clearUser() {
        user = null;
    }

    /**
     * 全局内存变量获取 versionControl
     */
    public Map<String, VersionControl> getVersionControl() {
        if (versionMap == null) {
            List<VersionControl> versionControls = getDaoSession().getVersionControlDao().queryBuilder().list();
            if (versionControls.size() <= 0) {
                return null;
            }
            versionMap = new HashMap<>();
            for (VersionControl versionControl : versionControls) {
                versionMap.put(versionControl.getName(), versionControl);
            }
        }
        return versionMap;
    }

    /**
     * 清除缓存 versionControl
     */
    public void clearVersionControl() {
        versionMap = null;
    }

    /**
     * 获取应用版本
     */
    public String getAppVersion() {
        return BuildConfig.VERSION_NAME;
    }
}
